package org.eduapp.trains.model.train;

import java.util.HashSet;
import java.util.Set;

import org.eduapp.trains.Constants;
import org.eduapp.trains.model.BaseModel;
import org.eduapp.trains.utils.UtilsAppCore;
import org.eduapp.trains.utils.UtilsString;
import org.eduapp.trains.vo.VoTrainSearch;

public class ModelTrain extends BaseModel{

	private static final long serialVersionUID = -5422833073511288091L;
	
	private String trainNumber;
	private String trainRuntime;
	private String trainStationStarting;
	private String trainStationTerminating;
	private String trainTimeDeparture;
	private String trainTimeArrival;
	private String trainType;
	private String trainRange;
	private String trainNumberEncode;
	private String trainLastUpdate;
	
	// temporary view value object
	private VoTrainSearch trainSearchVO = new VoTrainSearch();
	private ModelTrainSchedule trainScheduleStartingVO;
	private ModelTrainSchedule trainScheduleTerminationVO;
	private String trainScheduleSubRuntimeVO;
	private ModelTrainSchedule trainSchedule = null;
	
	private Set<ModelTrainSchedule> trainSchedules = new HashSet<ModelTrainSchedule>();
	
	public String getTrainNumber() {
		return trainNumber;
	}
	public void setTrainNumber(String trainNumber) {
		this.trainNumber = trainNumber;
	}
	public String getTrainRuntime() {
		return trainRuntime;
	}
	public void setTrainRuntime(String trainRuntime) {
		this.trainRuntime = trainRuntime;
	}
	public String getTrainStationStarting() {
		return trainStationStarting;
	}
	public void setTrainStationStarting(String trainStationStarting) {
		this.trainStationStarting = trainStationStarting;
	}
	public String getTrainStationTerminating() {
		return trainStationTerminating;
	}
	public void setTrainStationTerminating(String trainStationTerminating) {
		this.trainStationTerminating = trainStationTerminating;
	}
	public String getTrainTimeDeparture() {
		return trainTimeDeparture;
	}
	public void setTrainTimeDeparture(String trainTimeDeparture) {
		this.trainTimeDeparture = trainTimeDeparture;
	}
	public String getTrainTimeArrival() {
		return trainTimeArrival;
	}
	public void setTrainTimeArrival(String trainTimeArrival) {
		this.trainTimeArrival = trainTimeArrival;
	}
	public String getTrainType() {
		return trainType;
	}
	public void setTrainType(String trainType) {
		this.trainType = trainType;
	}
	public String getTrainRange() {
		return trainRange;
	}
	public void setTrainRange(String trainRange) {
		this.trainRange = trainRange;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public void setTrainSchedules(Set<ModelTrainSchedule> trainSchedules) {
		this.trainSchedules = trainSchedules;
	}
	public Set<ModelTrainSchedule> getTrainSchedules() {
		return trainSchedules;
	}
	public void setTrainNumberEncode(String trainNumberEncode) {
		this.trainNumberEncode = trainNumberEncode;
	}
	public String getTrainNumberEncode() {
		if (UtilsString.isNotEmpty(trainNumber)) {
			trainNumberEncode = trainNumber.indexOf("/") > -1 ? 
					trainNumber.replaceAll("/", Constants.DefaultTrainNumberEncode) : trainNumber;
		}
		return trainNumberEncode;
	}
	public void setTrainLastUpdate(String trainLastUpdate) {
		this.trainLastUpdate = trainLastUpdate;
	}
	public String getTrainLastUpdate() {
		return trainLastUpdate;
	}
	public void setTrainScheduleStartingVO(ModelTrainSchedule trainScheduleStartingVO) {
		this.trainScheduleStartingVO = trainScheduleStartingVO;
	}
	public ModelTrainSchedule getTrainScheduleStartingVO() {
		if (trainSearchVO != null && UtilsString.isNotEmpty(trainSearchVO.getTrainStationFrom())) {
			
			trainSearchVO.setTrainStationFrom(trainSearchVO.getTrainStationFrom().replace("վ", ""));
			trainSearchVO.setTrainStationTo(trainSearchVO.getTrainStationTo().replace("վ", ""));
			int match = 0;
			if (trainSchedules != null) {
				for (ModelTrainSchedule schedule : trainSchedules) {
					int indexTmp = schedule.getTrainStationName().indexOf(trainSearchVO.getTrainStationFrom()); 
					if (indexTmp > -1) {
						match = trainSearchVO.getTrainStationFrom().length();
						trainScheduleStartingVO = schedule; //.getTrainStationName();
						if (UtilsString.isNotEmpty(trainSearchVO.getTrainNumber())) {
							if (schedule.getTrainModel().getTrainNumber().equals(trainSearchVO.getTrainNumber())) {
								trainScheduleStartingVO = schedule;
								break;
							}
						} else if (schedule.getTrainStationName().length() == match) {
							break;
						}
					}
				}
			}
		}
		return trainScheduleStartingVO;
	}
	public void setTrainScheduleTerminationVO(ModelTrainSchedule trainScheduleTerminationVO) {
		this.trainScheduleTerminationVO = trainScheduleTerminationVO;
	}
	public ModelTrainSchedule getTrainScheduleTerminationVO() {
		if (trainSearchVO != null && UtilsString.isNotEmpty(trainSearchVO.getTrainStationTo())) {
			int match = 0;
			if (trainSchedules != null) {
				for (ModelTrainSchedule schedule : trainSchedules) {
					int indexTmp = schedule.getTrainStationName().indexOf(trainSearchVO.getTrainStationTo());
					if(trainSearchVO.getTrainSearchType()==null){
						indexTmp = schedule.getTrainStationName().equals(trainSearchVO.getTrainStationTo())? 0:-1;
					}
					if (indexTmp > -1) {
						match = trainSearchVO.getTrainStationTo().length();
						trainScheduleTerminationVO = schedule; //.getTrainStationName();
						if (UtilsString.isNotEmpty(trainSearchVO.getTrainNumber())) {
							if (schedule.getTrainModel().getTrainNumber().equals(trainSearchVO.getTrainNumber())) {
								trainScheduleTerminationVO = schedule;
								break;
							}
						} else if (schedule.getTrainStationName().length() == match) {
							break;
						}
					}
				}
			}
		}
		return trainScheduleTerminationVO;
	}
	public void setTrainSchedule(ModelTrainSchedule trainSchedule) {
		this.trainSchedule = trainSchedule;
	}
	public ModelTrainSchedule getTrainSchedule() {
		if (trainSchedules != null) {
			int count = 0;
			for (ModelTrainSchedule schedule : trainSchedules) {
				if (isSearchPrototype()) {
					if (count == trainSchedules.size()) {
						trainSchedule = schedule;
					}
				} else {
					if (schedule.getTrainStationName().equals(trainScheduleStartingVO.getTrainStationName())) {
						trainSchedule = schedule;
						break;
					}
				}
				count++;
			}
		}
		return trainSchedule;
	}
	
	/**
	 * laid on search prototype or not
	 * @return
	 */
	private boolean isSearchPrototype () {
		return trainSearchVO != null && UtilsString.isNotEmpty(trainSearchVO.getTrainStationFrom()) 
			&& UtilsString.isNotEmpty(trainSearchVO.getTrainStationTo());
	}
	public void setTrainSearchVO(VoTrainSearch trainSearchVO) {
		this.trainSearchVO = trainSearchVO;
	}
	public VoTrainSearch getTrainSearchVO() {
		if (trainSearchVO == null) {
			trainSearchVO = new VoTrainSearch();
		}
		return trainSearchVO;
	}
	public void setTrainScheduleSubRuntimeVO(String trainScheduleSubRuntimeVO) {
		this.trainScheduleSubRuntimeVO = trainScheduleSubRuntimeVO;
	}
	public String getTrainScheduleSubRuntimeVO() {
		if (trainScheduleStartingVO != null && trainScheduleTerminationVO != null) {
			trainScheduleSubRuntimeVO = UtilsAppCore.getTimeStrBySub(
					trainScheduleStartingVO.getTrainRuntime(), 
					trainScheduleTerminationVO.getTrainRuntime(),
					UtilsAppCore.getTimeMinByTrainTimeSub(
							trainScheduleStartingVO.getTrainArriveTime(),
							trainScheduleStartingVO.getTrainDepartureTime()));
		}
		return trainScheduleSubRuntimeVO;
	}
}
